package com.wefans.wefansapp.activity;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wefans.wefansapp.R;
import com.wefans.wefansapp.activity.MystarGroupAdapter.ViewHolder;
import com.wefans.wefansapp.common.tool.data.StarData;
import com.wefans.wefansapp.common.tool.data.StarGroupData;

public class MystarAdapter extends BaseAdapter {

	private ArrayList<StarData> mStarList = null;
    private LayoutInflater mInflater = null;
    private ViewHolder viewHolder = null;
    private MystarFavoriteListener mFavoriteListener = null;

	public MystarAdapter(Context c, ArrayList<StarData> starList, MystarFavoriteListener favoriteListener) {
		super();
		this.mInflater = LayoutInflater.from(c);
		this.mStarList = starList;
		this.mFavoriteListener = favoriteListener;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mStarList.size();
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mStarList.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = convertView;
        
        if(v == null){
            viewHolder = new ViewHolder();
            v = mInflater.inflate(R.layout.list_item_star, null);
            viewHolder.tv_starName = (TextView) v.findViewById(R.id.TV_START_NAME);
            viewHolder.iv_favorite = (ImageView) v.findViewById(R.id.IV_FAVORITE_STATE);
            v.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)v.getTag();
        }
        
        final StarData dtStar = (StarData) getItem(position);
        
        if (dtStar != null) {
        	viewHolder.tv_starName.setText(dtStar.getStrName());
        	viewHolder.iv_favorite.setSelected(dtStar.isFavorite());
        	viewHolder.iv_favorite.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				dtStar.setFavorite(!dtStar.isFavorite());
    				
    				if (mFavoriteListener != null) {
    					mFavoriteListener.onFavoriteChange(dtStar.getStrId(), String.format("%s", dtStar.getStrName()), dtStar.isFavorite());
    				}
    				notifyDataSetChanged();
    			}
    		});
        }
         
        return v;
	}

	class ViewHolder {        
        public TextView tv_starName;
        public ImageView iv_favorite;
    }
}
